package com.mcxx.modules.temporary.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.mcxx.modules.temporary.service.TempQueryService;
import com.mcxx.modules.temporary.service.TempRescueQueryExportService;
import com.mcxx.modules.miniSalvation.check.entity.CheckEntity;
import com.mcxx.modules.miniSalvation.check.service.read.CheckReadService;
import com.mcxx.modules.miniSalvation.order.dto.OrderInHouseDTO;
import com.mcxx.modules.miniSalvation.order.service.read.InHouseReadService;
import com.mcxx.modules.temporary.cancel.param.TmpCancelParam;
import com.mcxx.modules.temporary.cancel.service.read.TmpCancelReadService;
import com.mcxx.modules.temporary.family.dto.TmpFamilyMemberDTO;
import com.mcxx.modules.temporary.family.entity.TmpFamilyMemberEntity;
import com.mcxx.modules.temporary.family.param.SalvationFamilyParam;
import com.mcxx.modules.temporary.family.param.SalvationObjectParam;
import com.mcxx.modules.temporary.order.dto.ApplicationDTO;
import com.mcxx.modules.temporary.order.dto.TmpAuditDTO;
import com.mcxx.modules.temporary.order.dto.TmpOrderStatement;
import com.mcxx.modules.temporary.order.entity.TmpOrderDetailEntity;
import com.mcxx.modules.temporary.order.service.read.TmpOrderReadService;
import com.mcxx.modules.temporary.salary.service.read.TmpFamilySalaryReadService;
import com.mcxx.service.DefaultRestService;
import com.mcxx.util.Constant;
import com.mcxx.util.FileExportUtil;
import com.mcxx.util.ZipInfoUtil;
import com.yinhai.ta404.component.audit.vo.ExcelInfo;
import com.yinhai.ta404.core.restservice.annotation.RestService;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.validate.annotation.V;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;


@Validated
@Api("临时救助查询管理")
@RestService({"temporary/query/v2"})
public class TempSalvationQueryController extends DefaultRestService {

    private static final Logger logger = LoggerFactory.getLogger(TempSalvationQueryController.class);

    @Autowired
    private TmpOrderReadService orderReadService;
    @Autowired
    private InHouseReadService inHouseReadService;
    @Autowired
    private TmpFamilySalaryReadService familySalaryReadService;
    @Autowired
    private TempRescueQueryExportService tempRescueQueryExportService;
    @Autowired
    private TempQueryService tempQueryService;
    @Autowired
    private CheckReadService checkReadService;
    @Autowired
    private TmpCancelReadService tmpCancelReadService;

    
    @PostMapping("listSalvationFamily")
    @ApiOperation("查询救助家庭列表")
    public void listSalvationFamily(SalvationFamilyParam param, PageParam pageParam) {
        logger.info("查询查询救助家庭列表，参数：{},{}", JSON.toJSONString(param), JSON.toJSONString(pageParam));
        Page page = orderReadService.listSalvationFamily(param, pageParam, getCurUserAccount());
        setPageBean(page);
        logger.info("查询保障家庭列表完成，结果：{}", JSON.toJSONString(page));
    }

    
    @PostMapping("listSalvationObject")
    @ApiOperation("查询救助对象列表")
    public void listSalvationObject(SalvationObjectParam param, PageParam pageParam) {
        logger.info("查询救助对象列表，参数：{},{}", JSON.toJSONString(param),JSON.toJSONString(pageParam));
        Page page = orderReadService.listSalvationObject(param, pageParam, getCurUserAccount());
        setPageBean(page);
        logger.info("查询救助对象列表完成，结果：{}", JSON.toJSONString(page));
    }

    
    @GetMapping("getFamilyCountSummary")
    @ApiOperation("统计总户数、总人数、总金额")
    public void getFamilyCountSummary(SalvationFamilyParam param) {
        logger.info("统计总户数、总人数、总金额，参数是：{}", JSON.toJSONString(param));
        UserAccountVo accountVo = getCurUserAccount();
        Map data = orderReadService.getFamilyCountSummary(param,accountVo);
        setData(data);
        logger.info("查询保障对象列表结束，参数是：{}", JSON.toJSONString(data));
    }

    
    @GetMapping("getOrderDetail")
    @ApiOperation("查询业务详情打印费用结算单")
    public void getOrderDetail(@V({"notnull"})  String orderId) {
        logger.info("查询业务详情打印费用结算单，参数是：{}", orderId);
        UserAccountVo accountVo = getCurUserAccount();
        TmpOrderStatement tmpOrderStatement = orderReadService.selectOrderStatement(orderId);
        tmpOrderStatement.setArea(accountVo.getAreaName());
        setData(tmpOrderStatement);
        logger.info("查询保障对象列表结束，参数是：{}", JSON.toJSONString(tmpOrderStatement));
    }

    
    @PostMapping("exportOrderDetail")
    @ApiOperation("临时救助导出费用结算单")
    public void exportOrderDetail(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String orderId = request.getParameter("orderId");
        logger.info("临时救助导出费用结算单，参数是：{}", orderId);
        UserAccountVo accountVo = getCurUserAccount();
        ExcelInfo excelInfo = tempRescueQueryExportService.exportOrderDetail(orderId,accountVo.getAreaName());
        if(null != excelInfo){
            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }

    
    @GetMapping("getObjectCountSummary")
    @ApiOperation("根据条件统计救助对象总人数、总金额")
    public void getObjectCountSummary(SalvationObjectParam param) {
        logger.info("根据条件统计救助对象总人数、总金额，参数：{}", JSON.toJSONString(param));
        UserAccountVo accountVo = getCurUserAccount();
        Map data = orderReadService.getObjectCountSummary(param, accountVo);
        setData(data);
        logger.info("根据条件统计救助对象总人数、总金额完成，结果：{}", JSON.toJSONString(data));
    }

    

    
    @GetMapping("getOrderDetailById")
    @ApiOperation("查看未办结业务详情")
    public void getOrderDetailById(@V({"notnull"}) String orderId) {
        logger.info("查看申请详情，参数：{}", orderId);
        ApplicationDTO applyDto = tempQueryService.getOrderDetail(orderId);
        setData(applyDto);
        logger.info("查看未办结业务详情完成，结果：{}", applyDto);
    }

    
    @GetMapping("listFamilyMember")
    @ApiOperation("根据orderId获取家庭成员列表")
    public void listFamilyMember(@V({"notnull"}) String orderId) {
        logger.info("查询家庭成员列表，参数：{}", orderId);
        List list = tempQueryService.listFamilyMember(orderId);
        setData(list);
        logger.info("查询家庭成员列表完成，结果：{}", JSON.toJSONString(list));
    }

    
    @GetMapping("getMemberByIdAndOrderId")
    @ApiOperation("根据id查询指定家庭成员信息")
    public void getMemberByIdAndOrderId(@V({"notnull"}) String id, @V({"notnull"}) String orderId) {
        logger.info("根据memberId和orderId查询指定家庭成员信息，参数：id：{}，orderId：{}", id, orderId);
        TmpFamilyMemberEntity memberEntity = tempQueryService.getFamilyMember(id, orderId);
        setData(memberEntity);
        logger.info("根据id查询指定家庭成员信息完成，结果：{}", JSON.toJSONString(memberEntity));
    }

    
    @GetMapping("getMemberBankByIdAndOrderId")
    @ApiOperation("根据id查询指定家庭成员信息")
    public void getMemberBankByIdAndOrderId(@V({"notnull"}) String id, @V({"notnull"}) String orderId) {
        logger.info("根据memberId和orderId查询指定家庭成员信息，参数：id：{}，orderId：{}", id, orderId);
        TmpFamilyMemberDTO tmpFamilyMemberDTO = tempQueryService.getFamilyMemberBankInfo(id, orderId,Constant.BusinessCode.LINSHI);
        setData(tmpFamilyMemberDTO);
        logger.info("根据id查询指定家庭成员信息完成，结果：{}", JSON.toJSONString(tmpFamilyMemberDTO));
    }

    
    @GetMapping("listHouseholdSurvey")
    @ApiOperation("根据orderId查询入户调查记录列表（乡镇/区县）")
    public void listHouseholdSurvey(@V({"notnull"}) String orderId, @V({"notnull"}) String userAreaLevel) {
        logger.info("查询入户调查记录列表，参数：{}", orderId);
        List<OrderInHouseDTO> list = inHouseReadService.selectListByOrderId(orderId, userAreaLevel);
        setData(list);
        logger.info("查询入户调查记录列表完成，结果：{}", JSON.toJSONString(JSON.toJSONString(list)));
    }

    
    @GetMapping("getInHouseRecord")
    @ApiOperation("根据orderId查询入户调查记录（乡镇/区县）")
    public void getInHouseRecord(@V("notnull") String inHouseId){
        logger.info("根据orderId查询入户调查记录，参数是：{}", inHouseId);
        OrderInHouseDTO orderInHouseDTO = inHouseReadService.queryNewById(inHouseId);
        setData(orderInHouseDTO);
        logger.info("根据orderId查询入户调查记录，结果是：{}", orderInHouseDTO);
    }

    
    @GetMapping("getFamilySalaryInfo")
    @ApiOperation("查询家庭救助待遇信息")
    public void getFamilySalaryInfo(@V("notnull") String orderId){
        logger.info("查询家庭救助待遇信息，参数：{}", orderId);
        TmpAuditDTO tmpAuditDTO = familySalaryReadService.getFamilySalaryInfo(orderId);
        setData(tmpAuditDTO);
        logger.info("查询家庭救助待遇信息完成，结果：{}", JSON.toJSONString(tmpAuditDTO));
    }

    
    @GetMapping("getAuditInfo")
    @ApiOperation("查询审核/审批信息")
    public void getAuditInfo(@V("notnull") String orderId, String checkType){
        logger.info("查询审核/审批信息，参数：orderId:{},checkType:{}", orderId, checkType);
        TmpOrderDetailEntity data = orderReadService.getAuditInfo(orderId,checkType);
        setData(data);
        logger.info("查询审核/审批信息完成，结果：{}", JSON.toJSONString(data));
    }


    
    @GetMapping("getAuditInfos")
    @ApiOperation("查询审核信息")
    public void getSalvationAudits(@V("notnull") String orderId, String checkType) {
        logger.info("查询审核/审批信息，参数：orderId:{},checkType:{}", orderId, checkType);
        List<TmpOrderDetailEntity>  result = orderReadService.getAuditInfos(orderId,checkType);
        //List<OrderApproveDTO>  result = orderReadService.getAuditInfo(orderId, Constant.CheckDatailStatus.Pi,null);
        setData(result);
        logger.info("查询审核/审批信息完成，结果：{}", JSON.toJSONString(result));
    }


    

    
    
    @PostMapping({"downLoadRescueList"})
    public void exportOrgOpLog(HttpServletResponse response, @RequestBody SalvationObjectParam param) throws IOException {
        ExcelInfo excelInfo = tempRescueQueryExportService.exportExcel(param.getExportType(),param.getOrderIdsList(),param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
    }

    

    @ApiOperation("查询核对信息")
    @PostMapping("getCheckList")
    public void getCheckList(@RequestParam("orderId")String orderId){
        logger.info("查询核对列表信息，参数：{}", orderId);
        List<CheckEntity> entities = checkReadService.selectCheckList(orderId);
        setData(entities);
        logger.info("查询核对列表信息，参数：{}", JSON.toJSONString(entities));
    }
    
    @PostMapping("listSalvationCancel")
    @ApiOperation("查询审核作废列表")
    public void listSalvationCancel(TmpCancelParam param, PageParam pageParam) {
        logger.info("查询审核作废列表，参数：{},{}", JSON.toJSONString(param), JSON.toJSONString(pageParam));
        Page page = tmpCancelReadService.listSalvationCancel(param, pageParam, getCurUserAccount());
        setPageBean(page);
        logger.info("查询审核作废列表完成，结果：{}", JSON.toJSONString(page));
    }

    
    @PostMapping({"downLoadCancelList"})
    @ApiOperation("导出审核作废列表")
    public void downLoadCancelList(HttpServletResponse response, @RequestBody TmpCancelParam param) throws IOException {
        logger.info("开始导出审核作废列表，参数是：{}", param);
        ExcelInfo excelInfo = tempRescueQueryExportService.exportOrderCancel(param,getCurUserAccount());
        if(null != excelInfo){
            FileExportUtil.exportFileResponse(response, excelInfo, getCurUserAccount().getAreaName());
//            ZipInfoUtil.exportZipResponse(response, excelInfo, this.getCurUserId());
        }
        logger.info("导出审核作废列表完成");
    }

    
    @PostMapping({"getFamilySummaryForWxapp"})
    @ApiOperation("查询查询救助家庭列表微信查询")
    public void getFamilySummaryForWxapp( @V({"notnull"}) String param,@V({"notnull"}) String pageParam) {
        logger.info("查询查询救助家庭列表微信查询，参数是：{},{}", param,pageParam);
        SalvationFamilyParam salvationObjectParam = JSONObject.parseObject(param,SalvationFamilyParam.class);
        PageParam pageObject = JSONObject.parseObject(pageParam,PageParam.class);
        Page page = orderReadService.getFamilySummaryForWxapp(salvationObjectParam, pageObject);
        setPageBean(page);
        logger.info("查询保障家庭列表完成微信查询，结果：{}", JSON.toJSONString(page));
    }
}
